package com.whisperly.echotalk.starryb.netWorkUtils;

import android.util.Log;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;

import com.whisperly.echotalk.bllmodel.AiFeedback;
import com.whisperly.echotalk.bllmodel.BaseResponse;
import com.whisperly.echotalk.bllmodel.ChatHistory;
import com.whisperly.echotalk.bllmodel.ClearClientDto;
import com.whisperly.echotalk.bllmodel.CommonResponse;
import com.whisperly.echotalk.bllmodel.FindPasswordDto;
import com.whisperly.echotalk.bllmodel.FindPasswordResponse;
import com.whisperly.echotalk.bllmodel.LoginDto;
import com.whisperly.echotalk.bllmodel.LoginResponse;
import com.whisperly.echotalk.bllmodel.RegisterDto;
import com.whisperly.echotalk.bllmodel.RegisterResponse;
import com.whisperly.echotalk.bllmodel.ReportMessage;
import com.whisperly.echotalk.bllmodel.ResultBody;
import com.whisperly.echotalk.bllmodel.ReturnedValue;
import com.whisperly.echotalk.bllmodel.SignUp;
import com.whisperly.echotalk.bllmodel.UpdateInfo;
import com.whisperly.echotalk.bllmodel.UpdatePasswordDto;
import com.whisperly.echotalk.bllmodel.UserQuery;
import com.whisperly.echotalk.bllmodel.UserWithChat;
import com.whisperly.echotalk.starryb.utils.BaseURL;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.util.List;

@Keep
public class NetworkUtils {

    private static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BaseURL.API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    public static void postFeedback(String userName,String feedbackType, String feedbackContent, final NetworkCallback callback) {
        ApiService service = retrofit.create(ApiService.class);
        // 创建反馈对象
        AiFeedback feedback = new AiFeedback();
        feedback.setFeedbackType(feedbackType);
        feedback.setUserName(userName);
        feedback.setFeedbackContent(feedbackContent);


        Call<ReturnedValue> call = service.postFeedback(feedback);
        call.enqueue(new retrofit2.Callback<ReturnedValue>() {
            @Override
            public void onResponse(@NonNull Call<ReturnedValue> call, @NonNull retrofit2.Response<ReturnedValue> response) {
                if (response.isSuccessful()) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ReturnedValue> call, @NonNull Throwable t) {
                callback.onFailure(t);
            }
        });
    }

    public static void postReport(String message,String reportType, String reportDescription, final NetworkCallback callback) {
        ApiService service = retrofit.create(ApiService.class);

        ReportMessage reportMessage = new ReportMessage();
        reportMessage.setMessage(message);
        reportMessage.setReportType(reportType);
        reportMessage.setReportDescription(reportDescription);

        Call<ReturnedValue> call = service.postReport(reportMessage);
        call.enqueue(new retrofit2.Callback<ReturnedValue>() {
            @Override
            public void onResponse(@NonNull Call<ReturnedValue> call, @NonNull retrofit2.Response<ReturnedValue> response) {
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ReturnedValue> call, @NonNull Throwable t) {

                callback.onFailure(t);
            }
        });
    }

    public static void postLogin(LoginDto loginDto, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<LoginResponse>> login = service.login(loginDto);
        login.enqueue(new Callback<ResultBody<LoginResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<LoginResponse>> call, @NonNull retrofit2.Response<ResultBody<LoginResponse>> response) {
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<LoginResponse>> call, @NonNull Throwable t) {

                callback.onFailure(t);
            }
        });
    }

    public static void postRegister(RegisterDto registerDto,final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<RegisterResponse>> register = service.register(registerDto);
        register.enqueue(new Callback<ResultBody<RegisterResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<RegisterResponse>> call, @NonNull retrofit2.Response<ResultBody<RegisterResponse>> response) {
                Log.e("NetWork",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<RegisterResponse>> call, @NonNull Throwable t) {

                callback.onFailure(t);
            }
        });
    }

    public static void getDelete(String id, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.delete(id);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("NetWork",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {

                callback.onFailure(t);
            }
        });
    }

    public static void update(UserWithChat user, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.update(user);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }

    public static void updatePassword(UpdatePasswordDto user, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.updatePassword(user);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }

    public static void findPassword(FindPasswordDto user, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<FindPasswordResponse>> register = service.findPassword(user);
        register.enqueue(new Callback<ResultBody<FindPasswordResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<FindPasswordResponse>> call, @NonNull retrofit2.Response<ResultBody<FindPasswordResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<FindPasswordResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }

    public static void changePassword(UpdatePasswordDto user, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.changePassword(user);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }

    public static void loadDates(UserQuery user, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<BaseResponse<List<UserQuery>>>> register = service.loadDates(user);
        register.enqueue(new Callback<ResultBody<BaseResponse<List<UserQuery>>>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<BaseResponse<List<UserQuery>>>> call, @NonNull retrofit2.Response<ResultBody<BaseResponse<List<UserQuery>>>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<BaseResponse<List<UserQuery>>>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }

    public static void loadDatesByDate(UserQuery user, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<BaseResponse<List<UserQuery>>>> register = service.loadDatesByDate(user);
        register.enqueue(new Callback<ResultBody<BaseResponse<List<UserQuery>>>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<BaseResponse<List<UserQuery>>>> call, @NonNull retrofit2.Response<ResultBody<BaseResponse<List<UserQuery>>>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<BaseResponse<List<UserQuery>>>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }

    public static void getChatHistory(ChatHistory chatHistory, final  BlinkCallback callback){
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<BaseResponse<List<ChatHistory>>>> register = service.getChatHistory(chatHistory);
        register.enqueue(new Callback<ResultBody<BaseResponse<List<ChatHistory>>>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<BaseResponse<List<ChatHistory>>>> call, @NonNull retrofit2.Response<ResultBody<BaseResponse<List<ChatHistory>>>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<BaseResponse<List<ChatHistory>>>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }


    public static void saveChatHistory(ChatHistory chatHistory, final BlinkCallback callback) {
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.saveChatHistory(chatHistory);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    callback.onSuccess(response.body());
                } else {

                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                callback.onFailure(t);
            }
        });
    }

    public static void saveUserQuery(UserQuery userQuery, BlinkCallback blinkCallback) {
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.saveUserQuery(userQuery);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    blinkCallback.onSuccess(response.body());
                } else {

                    blinkCallback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                blinkCallback.onFailure(t);
            }
        });
    }

    public static void getUserWithChatByName(UserWithChat userWithChat, BlinkCallback blinkCallback) {
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.getUserWithChatByName(userWithChat);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    blinkCallback.onSuccess(response.body());
                } else {

                    blinkCallback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                blinkCallback.onFailure(t);
            }
        });
    }

    public static void deleteBatch(ClearClientDto clearClientDto, BlinkCallback blinkCallback) {
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.deleteBatch(clearClientDto);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    blinkCallback.onSuccess(response.body());
                } else {

                    blinkCallback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                blinkCallback.onFailure(t);
            }
        });
    }

    public static void signUp(SignUp signUp, BlinkCallback blinkCallback) {
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<CommonResponse>> register = service.signUp(signUp);
        register.enqueue(new Callback<ResultBody<CommonResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull retrofit2.Response<ResultBody<CommonResponse>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    blinkCallback.onSuccess(response.body());
                } else {

                    blinkCallback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<CommonResponse>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                blinkCallback.onFailure(t);
            }
        });
    }

    public static void getByUserId(String userId, BlinkCallback blinkCallback) {
        ApiService service = retrofit.create(ApiService.class);
        Call<ResultBody<BaseResponse<SignUp>>> register = service.getByUserId(userId);
        register.enqueue(new Callback<ResultBody<BaseResponse<SignUp>>>() {
            @Override
            public void onResponse(@NonNull Call<ResultBody<BaseResponse<SignUp>>> call, @NonNull retrofit2.Response<ResultBody<BaseResponse<SignUp>>> response) {
                Log.e("response",response.toString());
                if (response.isSuccessful()) {

                    blinkCallback.onSuccess(response.body());
                } else {

                    blinkCallback.onFailure(new IOException("Unexpected code " + response));
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResultBody<BaseResponse<SignUp>>> call, @NonNull Throwable t) {
                Log.e("Throwable",t.toString());
                blinkCallback.onFailure(t);
            }
        });
    }
}
